/**
 * 上传文件GRPC接口定义，待主进程调用与preload预加载脚本通信
 */

const grpc = require('grpc');
const { FileUploadServiceClient, StateServiceClient } = require('../grpc/Upload_grpc_pb'); // 你的 gRPC 客户端文件路径
const { readConfigFile } = require('../utils/getBaseUrl')
// console.log('1', readConfigFile())
const config = readConfigFile()
// console.log('config', config);
// const ip = '192.168.100.189:50051'
// const ip = '127.0.0.1:50051'
const ip = config.grpc_baseUrl || ""

//创建一个不安全连接，需确定是否使用安全传输：TLS，OAuth，JWT，自定义
const createInsecure = grpc.credentials.createInsecure()

/**
 * @returns 创建 gRPC 客户端实例并指定 gRPC 服务端点和凭据
 */
function createGRPCClientSet() {
  const grpcClient = new FileUploadServiceClient(ip, createInsecure)
  return grpcClient
}
function createGRPCClientGet() {
  const grpcClient = new StateServiceClient(ip, createInsecure)
  return grpcClient;
}


// 获取上传列表
function getUploadList(request) {
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientGet();
    grpcClient.getUploadList(request, (error, response) => {
      if (error) {
        reject(error); // 如果发生错误，拒绝 Promise
      } else {
        resolve(response); // 如果成功，解决 Promise
      }
    });
  });
}
// 上传文件
function addUpload(request) {
  console.log('addUpload', request);
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientSet();
    grpcClient.addUpload(request, (error, response) => {
      if (error) {
        reject(error); // 如果发生错误，拒绝 Promise
      } else {
        resolve(response); // 如果成功，解决 Promise
      }
    });
  });
}
// 暂停上传
function pauseUpload(request) {
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientSet();
    grpcClient.pauseUpload(request, (error, response) => {
      if (error) {
        reject(error); // 如果发生错误，拒绝 Promise
      } else {
        resolve(response); // 如果成功，解决 Promise
      }
    });
  });
}
// 恢复上传
function resumeUpload(request) {
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientSet();
    grpcClient.resumeUpload(request, (error, response) => {
      if (error) {
        reject(error); // 如果发生错误，拒绝 Promise
      } else {
        resolve(response); // 如果成功，解决 Promise
      }
    });
  });

}
// 移除上传
function deleteUpload(request) {
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientSet();
    grpcClient.deleteUpload(request, (error, response) => {
      if (error) {
        reject(error); // 如果发生错误，拒绝 Promise
      } else {
        resolve(response); // 如果成功，解决 Promise
      }
    });
  });
}
// 根据id获取状态
function getStateByID(request) {
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientGet();
    grpcClient.getStateByID(request, (error, response) => {
      if (error) {
        reject(error); // 如果发生错误，拒绝 Promise
      } else {
        resolve(response); // 如果成功，解决 Promise
      }
    });
  });
}
// 获取上传完成列表
function getFinishUploadList(request) {
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientGet();
    grpcClient.getFinishUploadList(request, (error, response) => {
      if (error) {
        reject(error); // 如果发生错误，拒绝 Promise
      } else {
        resolve(response); // 如果成功，解决 Promise
      }
    });
  });
}
// 删除全部上传列表
function deleteAllUpload(request) {
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientSet()
    grpcClient.deleteAllUpload(request, (error, response) => {
      if (error) {
        reject(error)
      } else {
        resolve(response)
      }
    })
  })
}
// 暂停全部上传列表
function pauseAllUpload(request) {
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientSet()
    grpcClient.pauseAllUpload(request, (error, response) => {
      if (error) {
        reject(error)
      } else {
        resolve(response)
      }
    })
  })
}
// 恢复全部上传列表
function resumeAllUpload(request) {
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientSet()
    grpcClient.resumeAllUpload(request, (error, response) => {
      if (error) {
        reject(error)
      } else {
        resolve(response)
      }
    })
  })
}
// 设置传输设置
function setTransSetting(request) {
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientSet()
    grpcClient.setTransSetting(request, (error, response) => {
      if (error) {
        reject(error)
      } else {
        resolve(response)
      }
    })
  })
}
// 获取传输设置
function getTransSetting(request) {
  return new Promise((resolve, reject) => {
    const grpcClient = createGRPCClientSet()
    grpcClient.getTransSetting(request, (error, response) => {
      if (error) {
        reject(error)
      } else {
        resolve(response)
      }
    })
  })
}
module.exports = {
  getUploadList,
  addUpload,
  pauseUpload,
  resumeUpload,
  deleteUpload,
  getStateByID,
  getFinishUploadList,
  deleteAllUpload,
  pauseAllUpload,
  resumeAllUpload,
  setTransSetting,
  getTransSetting
};
